package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.PlatformResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.entity.*;
import com.zito.health.service.ItemService;
import com.zito.health.service.ItemTechnicalReportService;
import com.zito.health.service.ItemUndertakeService;
import com.zito.health.service.UnitService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.ItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 项目信息
 */
@Slf4j
@RestController
@RequestMapping("/item/info")
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemUndertakeService itemUndertakeService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private ItemTechnicalReportService itemTechnicalReportService;

    /**
     * 保存
     * @param item
     * @return
     */
    @PostMapping("/save")
    public PlatformResult save(Item item){
        item.setExamineTime(new Date());
        item.setDeclareTime(new Date());
        item.setTypes(1);
        User current = CacheUtil.getCacheUser().getUser();
        item.setUserId(current.getUserid());
        item.setAreaId(current.getAreaId());
        item = itemService.save(item);
        Unit unit= new Unit();
        unit.setId(current.getUnitId());
        unit = unitService.queryOne(unit);
        ItemUndertake itemUndertake= new ItemUndertake();
        BeanUtils.copyProperties(unit , itemUndertake);
        itemUndertake.setItemId(item.getId());
        itemUndertake.setUnitId(unit.getId());
        itemUndertake.setId(null);
        itemUndertakeService.save(itemUndertake);//承担单位保存
        return PlatformResult.success(item.getId());
    }

    @PostMapping("/update")
    public PlatformResult update(Item item){
        itemService.update(item);
        return PlatformResult.success();
    }

    /**
     * 立项专家提交评审
     * @return
     * @auth zhao / sun UpdateTime2020-06-19
     */
    @PutMapping("/lxExpertSub")
    public VerifyResult  lxExpertSub(ItemExpertDeclare itemExpertDeclare){
        VerifyResult verifyResult = new VerifyResult();
        String res = itemService.lxExpertSub(itemExpertDeclare);
        if(!"ok".equals(res)){
            verifyResult.setResult(false);
            verifyResult.setMsg(res);
            return verifyResult;
        }
        return verifyResult;
    }
    /**
     * 结题专家提交评审
     * @return
     */
    @PutMapping("/jtExpertSub")
    public VerifyResult  jtExpertSub(ItemExpertConclusion  itemExpertConclusion){
        VerifyResult verifyResult = new VerifyResult();
        String res=itemService.jtExpertSub(itemExpertConclusion);
        if(!"ok".equals(res)){
            verifyResult.setResult(false);
            verifyResult.setMsg(res);
            return verifyResult;
        }
        return verifyResult;
    }


    /**
     *立项提交(科研人员、科研单位、县区局)
     * @param item
     * @return
     */
    @PostMapping("/submitItem")
    public VerifyResult submitItem(Item item){
        VerifyResult verifyResult = new VerifyResult();
        User current = CacheUtil.getCacheUser().getUser();
        if(current.getRoleId()==5){//科研人员提交
            String res = itemService.canSubmit(item);
            if(!"ok".equals(res)){
                verifyResult.setResult(false);
                verifyResult.setMsg(res);
                return verifyResult;
            }
//            item.setDeclareTime(new Date());
        }else if(current.getRoleId()==10){//县区提交
            item.setStates(1);//未审核
        } else {//单位提交
            if(current.getAreaId() ==370200){//委直属
                item.setStates(1);//未审核
            }else{//县/区直属
                item.setStates(7);//县区未提交
            }
        }

        itemService.update(item);
        return verifyResult;
    }

    /**
     * 提交结题（科研人员，科研单位，县区局）
     * @param item
     * @return
     */
    @PostMapping("/submitConclusion")
    public PlatformResult submitConclusion(Item item){
        User current = CacheUtil.getCacheUser().getUser();
        if(current.getRoleId()==5){//科研人员提交
            Integer flag =itemService.submitConclusion(item);
            if(flag != 1){
                return  PlatformResult.failure("进度更新到100%才能提交结题");
            }
            item.setStates(6);//单位未提交
        }else if(current.getRoleId()==10) {//县区提交
            item.setStates(1);//未审核
        }else{//单位提交
            if(current.getAreaId() ==370200){//委直属
                item.setStates(1);//未审核
            }else{//县/区直属
                item.setStates(7);//县区未提交
            }
        }

        itemService.update(item);

        return PlatformResult.success();
    }

    /**
     * 立项、结题列表
     * @param item
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/list")
    public PlatformResult list(ItemVo item, @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        PageResult<ItemVo> result = new PageResult();
        User current = CacheUtil.getCacheUser().getUser();
        if(current.getRoleId() == 4){//专家
            item.setExpertId(current.getUserid());
        }else{
            if(item.getTypes()==1){//立项

                switch (current.getRoleId()){
                    case 1://admin
                        item.setStatesArray("0,6,7,1,2,3");
                        break;
                    case 2://市局
                        item.setStatesArray("1,2,3");
                        break;
                    case 5://科研人员
                        item.setUserId(current.getUserid());
                        item.setStatesArray("0,6,7,1,2,3");
                        break;
                    case 8://科研单位
                        item.setUnitId(current.getUnitId());
                        item.setStatesArray("6,7,1,2,3");
                        break;
                    case 10://区县科研
                        item.setAreaId(current.getAreaId());
                        item.setStatesArray("7,1,2,3");
                        break;
                }

            }else if(item.getTypes()==2){//结题
                switch (current.getRoleId()){
                    case 1://admin
                        item.setStatesArray("6,7,1,2,3,4");
                        break;
                    case 2://市局
                        item.setStatesArray("1,2,3,4");
                        break;
                    case 5://科研人员
                        item.setUserId(current.getUserid());
                        item.setStatesArray("6,7,1,2,3,4");
                        break;
                    case 8://科研单位
                        item.setUnitId(current.getUnitId());
                        item.setStatesArray("6,7,1,2,3,4");
                        break;
                    case 10://区县科研
                        item.setAreaId(current.getAreaId());
                        item.setStatesArray("7,1,2,3,4");
                        break;
                }

            }else{//types=3时，是科研项目总览的
                item.setTypes(null);
                switch (current.getRoleId()){
                    case 1://admin
                        item.setStatesArray("0,6,7,1,2,3");
                        break;
                    case 2://市局
                        item.setStatesArray("1,2,3,4");
                        break;
                    case 5://科研人员
                        item.setUserId(current.getUserid());
                        break;
                    case 8://科研单位
                        item.setUnitId(current.getUnitId());
                        item.setStatesArray("6,7,1,2,3,4");
                        break;
                    case 10://区县科研
                        item.setAreaId(current.getAreaId());
                        item.setStatesArray("7,1,2,3,4");
                        break;
                }

            }

        }

        if(pageNo!=null&&pageSize!=null){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<ItemVo> list = itemService.queryList(item);
        PageInfo<ItemVo> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return PlatformResult.success(result);
    }

    /**
     * 进度列表
     * @param item
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/listProgress")
    public PlatformResult listProgress(ItemVo item, @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        PageResult<ItemVo> result = new PageResult();
        User current = CacheUtil.getCacheUser().getUser();

        switch (current.getRoleId()){
            case 5://科研人员
                item.setUserId(current.getUserid());
                break;
            case 8://科研单位
                item.setUnitId(current.getUnitId());
                break;
            case 10://区县科研
                item.setAreaId(current.getAreaId());
                break;
        }

        if(pageNo!=null&&pageSize!=null){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<ItemVo> list = itemService.queryList(item);
        PageInfo<ItemVo> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return PlatformResult.success(result);
    }

    @PostMapping("/query")
    public PlatformResult query(Integer id){
        return PlatformResult.success(itemService.queryById(id));
    }

    @PostMapping("/delete")
    public PlatformResult delete(Integer id){
        itemService.delete(id);
        return PlatformResult.success();
    }

    @PostMapping("/expertList")
    public PlatformResult expertList(ItemVo item, @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                     @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        PageResult<ItemVo> result = new PageResult();
        User current = CacheUtil.getCacheUser().getUser();
        item.setExpertId(current.getUserid());
        if(pageNo!=null&&pageSize!=null){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<ItemVo> list = itemService.queryList(item);
        PageInfo<ItemVo> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return PlatformResult.success(result);
    }

    @PostMapping("/info")
    public PlatformResult info(Long id){
        return PlatformResult.success(itemService.info(id));
    }

    /**
     * admin退回科研
     * auth sun
     */
    @PutMapping("/sendItem")
    public PlatformResult sendItem(Item item){
        item = itemService.queryById(item.getId());
        itemService.sendItem(item);
        return PlatformResult.success();
    }

    /**
     * 技术报告
     * @auth sun
     */
    @PutMapping("/upReport")
    public PlatformResult upReport(ItemTechnicalReport itemTechnicalReport){
        ItemTechnicalReport itemTechnicalReportQuery = new ItemTechnicalReport();
        itemTechnicalReportQuery.setItemId(itemTechnicalReport.getItemId());
        itemTechnicalReportQuery = itemTechnicalReportService.queryOne(itemTechnicalReportQuery);
        // ==null 未上传过
        if (itemTechnicalReportQuery == null) {
            itemTechnicalReportService.save(itemTechnicalReport);
        }else{
            itemTechnicalReport.setId(itemTechnicalReportQuery.getId());
            itemTechnicalReportService.update(itemTechnicalReport);
        }
        return PlatformResult.success();
    }

    /**
     * 查询技术报告
     * @auth sun
     */
    @GetMapping("/queryReport")
    public PlatformResult queryReport(ItemTechnicalReport itemTechnicalReport){
        itemTechnicalReport = itemTechnicalReportService.queryOne(itemTechnicalReport);
        return PlatformResult.success(itemTechnicalReport);
    }
}
